﻿using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using TMPro;
using TMPro;
using System.Text;
using UnityEngine.Serialization;
using static CyberU;
using static CyberU.UI;

public class TextInput : SimpleText
{
    public TMP_InputField textInputField
    {
        get{return inputField;}
    }
    public Image image;
    public GameObject textAreaGameObject;
    public GameObject placeholderGameObject;
    public TextMeshProUGUI placeholderText;
    public LayoutElement layoutElement;
    public GameObject textGameObject;
    public string defaultText = "Click to input ...";

    public TextMeshProUGUI binedTextComponent
    {
        get=>textComponent;
        set=>textComponent = value;
    }
    public void Awake()
    {
        textAreaGameObject = FindGameObjectInChild(parent:gameObject,name:"Text Area");
        if (textAreaGameObject != null)
        {
            placeholderGameObject = FindGameObjectInChild(parent:textAreaGameObject,name:"Placeholder");
            textGameObject = FindGameObjectInChild(parent:textAreaGameObject,name:"Text");
            placeholderText=GetOrAddComponent<TextMeshProUGUI>(placeholderGameObject);
            layoutElement=GetOrAddComponent<LayoutElement>(placeholderGameObject);
            binedTextComponent = GetOrAddComponent<TextMeshProUGUI>(textGameObject);
            binedTextComponent.font=defaultFont;
            // Delog("inited.");
            placeholderText.text = defaultText;
            placeholderText.font=defaultFont;
        }
        inputField = GetOrAddComponent<TMP_InputField>(gameObject);
        image = GetOrAddComponent<Image>(gameObject);
        image.color=new Color(0,0,0,0);
        textInput = this;
        base.Awake();
        inputField.onValueChanged.AddListener((string newString)=>{onTextChanged.Invoke(text, newString);});
    }
}